﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Marbles.Resources
{
    /// <summary>
    /// Provides system utilities for Retrieval of embedded Resource Contents
    /// </summary>
    public static class ResourceUtility
    {
        public static List<string> GetManifestResourceNames()
        {
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            List<string> resources = new List<string>(assembly.GetManifestResourceNames());
            return resources;
        }

        public static bool ResourceExists(string name, System.Reflection.Assembly assembly)
        {
            string[] resources = assembly.GetManifestResourceNames();
            return resources.Contains(name);
        }

        public static string GetEmbeddedResourceValue(string name)
        {
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            if (!ResourceExists(name, assembly)) { return string.Empty; }

            Stream stream = assembly.GetManifestResourceStream(name);
            StreamReader sr = new StreamReader(stream);
            string value = sr.ReadToEnd();
            sr.Close();
            return value;
        }

        public static System.Drawing.Image GetEmbeddedResourceImage(string name)
        {
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            if (!ResourceExists(name, assembly)) { return null; }

            Stream stream = assembly.GetManifestResourceStream(name);
            System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
            stream.Close();
            return img;
        }
    }
}
